package com.colin.jipay.nxys;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import com.colin.jipay.BaseActivity;
import com.colin.jipay.R;
import com.colin.jipay.nxys.helper.NxysHelper;
import com.colin.jipay.nxys.websocket.AddReqPayQrCodeInterface;
import com.colin.jipay.nxys.websocket.NxysSocketService;
import com.colin.jipay.starpos.bean.CreateQrcodeBean;
import com.colin.jipay.utils.GsonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NxysMainActivity extends BaseActivity implements AddReqPayQrCodeInterface {

    @BindView(R.id.btnStartInit)
    Button btnStartInit;
    @BindView(R.id.tvContent)
    TextView tvContent;

    public static NxysSocketService.SocketBinder socketBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nxys_main);
        ButterKnife.bind(this);
    }

    /**
     * 执行农信易扫的一些数据初始化
     */
    private void initNxys(){
        //绑定socketService
//        bindSocketService();
        reqQrCodeUrl("{\"code_type\":\"nxyspay\",\"amount\":\"0.01\"}");
    }

    private void bindSocketService() {
        Intent intent =new Intent(this,NxysSocketService.class);
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                socketBinder= (NxysSocketService.SocketBinder) service;
                socketBinder.service_connect_Activity();
                socketBinder.addReqPayQrCodeInterface(NxysMainActivity.this);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, Context.BIND_AUTO_CREATE);
    }

    /**
     * 服务端发送请求获取收款的二维码地址
     * @param s 服务端发送过来的金额和提示语
     * {"code_type":"nxyspay","amount":"0.01"}
     */
    @Override
    public void reqQrCodeUrl(String s) {
        CreateQrcodeBean qrcodeBean= GsonUtils.fromJson(s,CreateQrcodeBean.class);
        Intent intent = new Intent(NxysHelper.ACTION_CONNECT);
        String mount = qrcodeBean.getAmount();
        String mark = qrcodeBean.getMark();
        if (!TextUtils.isEmpty(mount)) {
            intent.putExtra("money", mount);
        }
        if (!TextUtils.isEmpty(mark)) {
            intent.putExtra("mark", mark);
        }
        sendBroadcast(intent);
    }

    @OnClick(R.id.btnStartInit)
    public void onViewClicked() {
        initNxys();
    }
}
